package GUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class ZhuanPanel extends JPanel implements ActionListener {
    private ImageIcon imageIcon = Utilimg.getIcon("/img/buttonOff.jpg");
    private ImageIcon imageIcon1 = Utilimg.getIcon("/img/buttonSure.jpg");
    private JButton loginjButton;
    private Text2 frame;
    private JButton loginjButton1;
    private JTextField jTextField;
    private JTextField jTextField1;

    public ZhuanPanel(Text2 frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);

        JLabel jLabel = new JLabel("转账人:");
        jLabel.setBounds(200, 150, 150, 50);
        jLabel.setFont(new Font("宋体", Font.BOLD, 30));
        this.add(jLabel);

        JLabel jLabel2 = new JLabel("金额:");
        jLabel2.setBounds(200, 200, 150, 50);
        jLabel2.setFont(new Font("宋体", Font.BOLD, 30));
        this.add(jLabel2);

        jTextField = new JTextField();
        jTextField.setBounds(350, 150, 200, 30);
        this.add(jTextField);

        jTextField1 = new JTextField();
        jTextField1.setBounds(350, 200, 200, 30);
        this.add(jTextField1);
        //转账
        loginjButton1 = new JButton(imageIcon1);
        loginjButton1.setBounds(0, 350, 100, 50);
        this.add(loginjButton1);
        loginjButton1.addActionListener(this);
        //退出
        loginjButton = new JButton(imageIcon);
        loginjButton.setBounds(690, 350, 100, 50);
        this.add(loginjButton);
        loginjButton.addActionListener(this);
    }

    public void zhuan() {
//        String a=jTextField.getText();
//        double b=Double.valueOf(jTextField1.getText());
//        for(int i=0;i<frame.getList().size();i++){
//            if(a.equals(frame.getList().get(i).getAccount())&&a!=frame.getLoginUser().getAccount()){
//                if(b>frame.getLoginUser().getBalance()){
//                    JOptionPane.showMessageDialog(this,"余额不足");
//                }else if(b<frame.getLoginUser().getBalance()){
//                    frame.getLoginUser().setBalance(frame.getLoginUser().getBalance()-b);
//                    JOptionPane.showMessageDialog(this,"转账成功");
//                }
//            }
//            JOptionPane.showMessageDialog(this,"账号输入错误");
//            break;
//        }
        // 获取输入的对方账号
        String otherAccount = jTextField.getText();
        double blance = 0;
        try {
            // 获取输入的转账金额
            blance = Double.parseDouble(jTextField1.getText());
        }catch(NumberFormatException ex){
            JOptionPane.showMessageDialog(this,"转账金额必须为数字，并不能为空");
            return;
        }

        // 当期登录的账户
        DBUtil db =new DBUtil();
        User loginUser = db.getById(frame.getLoginUser().getId());
        User otherUser = db.getByUsername(otherAccount);
//        ArrayList<User> users = frame.getList();
//
//        boolean isExist = false;
//        for(int i = 0;i < users.size();i++){
//            if(users.get(i).getAccount().equals(otherAccount)){
//                isExist = true;
//                otherUser = users.get(i);
//                break;
//            }
//        }
        // 判断账号是否存在
        if(otherAccount==null){
            JOptionPane.showMessageDialog(this,"查无此人");
            return;
        }
        // 判断是否转给自己
        if(loginUser.getAccount().equals(otherAccount)){
            JOptionPane.showMessageDialog(this,"不能转给自己");
            return;
        }
        // 判断余额是否足够
        if(blance > loginUser.getBalance()){
            JOptionPane.showMessageDialog(this,"余额不足");
            return;
        }

        // 当期账户扣钱
        loginUser.setBalance(loginUser.getBalance() - blance);
        db.update(loginUser);
        // 对方账户加钱
        otherUser.setBalance(otherUser.getBalance() + blance);
        db.update(otherUser);
        db.trans(loginUser,otherUser);
        JOptionPane.showMessageDialog(this,"转账成功");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == loginjButton) {
            frame.switchPanel("menu");
        }else if(e.getSource()==loginjButton1){
            zhuan();
        }
    }
}
